package com.ccl.iot.device;

import android.graphics.Bitmap;
import android.text.TextUtils;

import com.ccl.iot.R;
import com.ccl.iot.bean.CodeviewBean;
import com.ccl.iot.bean.IrToBatchItemBean;
import com.ccl.iot.dvr.utils.Consts;
import com.ccl.iot.dvr.utils.HDImageManager;
import com.ccl.iot.ir.BlirConst;
import com.ccl.iot.ir.DBUtils;
import com.ccl.iot.privates.InfoProcessor;
import com.ccl.iot.publics.UIUtils;

import java.util.HashMap;
import java.util.Set;

import cn.com.broadlink.sdk.data.controller.BLDNADevice;

/**
 * 创建者     王浩
 * 创建时间   2017/5/19 14:47
 * 描述	     博联红外设备的子设备
 */

public class IOTSubBLir extends IOTVirtualDevice {

    private BLDNADevice mBLDNADevice;       //博联SDK获取到的设备
    private long mDbId;                     //数据库的id
    private long mSubId;                    //数据库中的子设备id
    private String mDevType;                //数据库中的子设备类型
    private IOTBLir mSuperDev;              //当前子设备的父设备
    private HashMap<String, CodeviewBean> mIrCodes = null;      //当前子设备的红外码集合<按键类型(或者按键名), 红外码的javabean>
    private boolean mTmp;           //是否是临时的子设备
    private String mTmpName;
    private IOTBLir.OnCodeChangedListener mCodeChangedListener = null;
    private String mUdid = null;
    private CodeviewBean mBatchCodes = null;      //情景模式中选中此子设备的红外码集合<案件类型, 红外码的javabean>

    private OnCheckedChangeListener mOnCheckedChangeListener;
    private OnCheckedClearListener mOnCheckedClearListener;

    public interface OnCheckedChangeListener {
        public void onCheckedChange();
    }

    public interface OnCheckedClearListener {
        public void onCheckedClear();
    }

    /**
     * 创建临时的子设备(子设备为没有学习过按键红外码也没有保存到服务器的设备为临时设备)
     *
     * @param devType
     * @param name
     * @param superDev
     */
    public IOTSubBLir(String devType, String name, IOTBLir superDev) {
        super(0);
        mDevType = devType;
        mTmp = true;
        mTmpName = name;
        mSuperDev = superDev;
        mIrCodes = new HashMap<String, CodeviewBean>();
    }

    /**
     * 创建真正的子设备
     *
     * @param aDevID
     * @param dbId
     * @param subid
     * @param devType
     * @param irDev
     * @param registerInterface
     * @param superDev
     */
    public IOTSubBLir(long aDevID, long dbId, long subid, String devType, BLDNADevice irDev, IOTRegisterInterface registerInterface, IOTBLir superDev) {
        super(aDevID);
        mTmp = false;
        mDbId = dbId;
        mSubId = subid;
        mDefIndex = (int) (mSubId+2);
        mDevType = devType;
        mBLDNADevice = irDev;
        setRegisterInterface(registerInterface);
        mSuperDev = superDev;
        mIrCodes = new HashMap<String, CodeviewBean>();
//        this.setAuth(IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_FULL);       //设置权限为完全控制
        this.setOnline(true);       //针对本app架构设置为当前设备一直在线
    }

    public long getSubId() {
        return mSubId;
    }

    public void delete() {
        if (mSuperDev != null) {
            mSuperDev.deleteSubDev(this);
        }
    }

    public void setOnCodeChangedListener(IOTBLir.OnCodeChangedListener l) {
        mCodeChangedListener = l;
    }

    /**
     * 添加一个按键的红外码库
     *
     * @param bean
     */
    public void setIRCode(CodeviewBean bean) {
        mIrCodes.put(bean.KeyType, bean);
        if (mCodeChangedListener != null) {
            mCodeChangedListener.onCodeChanged(null, null, false);
        }
    }

    public HashMap<String, CodeviewBean> getIRCode() {
        return mIrCodes;
    }

    public String getDevType() {
        return mDevType;
    }

    public String getDid() {
        return mSuperDev.getDid();
    }

    public String getPid() {
        return mSuperDev.getPid();
    }

    public long getDbId() {
        return mSuperDev.getDbId();
    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener l) {
        mOnCheckedChangeListener = l;
    }

    public void setOnCheckedClearListener(OnCheckedClearListener l) {
        mOnCheckedClearListener = l;
    }

    /**
     * 获取名字, 如果当前设备是临时的子设备,则返回临时名字, 如果不是临时设备则返回从服务器获取的名字
     *
     * @return
     */
    @Override
    public String getName() {
        if (mTmp && mTmpName != null) {
            return mTmpName;
        }
        if (mName == null) {
            mName = InfoProcessor.getInstance().getInfo(getID(), getNameLabel(), false);
            if (mName == null) {
                String typeName = getTypeName();
                if(typeName != null){
                    return typeName;
                }
            }
        }
        return mName;
    }

    private String getTypeName() {
        switch (mDevType) {
            case BlirConst.IR_SUBDEV_TYPE_TV:
                return UIUtils.getString(R.string.appliance_tv);
            case BlirConst.IR_SUBDEV_TYPE_PLAYER:
                return UIUtils.getString(R.string.appliance_player);
            case BlirConst.IR_SUBDEV_TYPE_AIRCONDITION:
            case BlirConst.IR_SUBDEV_TYPE_NON_STATE_AIRCONDITION:
                return UIUtils.getString(R.string.Air);
            case BlirConst.IR_SUBDEV_TYPE_STB:
                return UIUtils.getString(R.string.appliance_stb);
            case BlirConst.IR_SUBDEV_TYPE_PROJECTOR:
                return UIUtils.getString(R.string.appliance_projector);
            case BlirConst.IR_SUBDEV_TYPE_AMPLIFIER:
                return UIUtils.getString(R.string.appliance_projector);
            case BlirConst.IR_SUBDEV_TYPE_FAN:
                return UIUtils.getString(R.string.appliance_fan);
            case BlirConst.IR_SUBDEV_TYPE_BOX:
                return UIUtils.getString(R.string.appliance_box);
            case BlirConst.IR_SUBDEV_TYPE_LAMP:
                return UIUtils.getString(R.string.appliance_light);
            case BlirConst.IR_SUBDEV_TYPE_AIRPURIFIER:
                return UIUtils.getString(R.string.appliance_air_cleaner);
            case BlirConst.IR_SUBDEV_TYPE_SLR:
                return UIUtils.getString(R.string.appliance_slr);
            case BlirConst.IR_SUBDEV_TYPE_WATER_HEATER:
                return UIUtils.getString(R.string.appliance_water_heater);
            case BlirConst.IR_SUBDEV_TYPE_OTHER:
                return UIUtils.getString(R.string.custom);
        }
        return null;
    }

    public boolean isTmp() {
        return mTmp;
    }

    public IOTBLir getSuperDev() {
        return mSuperDev;
    }

    /**
     * 设置真实的在线状态
     *
     * @param online 通过博联sdk获取到的在线状态
     */
    public void setRealOnline(boolean online) {
        if (mSuperDev != null) {
            mSuperDev.setRealOnline(online);
        }
    }

    public boolean getRealOnline() {
        if (mSuperDev != null) {
            return mSuperDev.getRealOnline();
        }
        return false;
    }

    @Override
    public String getUDID() {
        if (mUdid == null) {
            mUdid = super.getUDID() + "@" + mSubId;
        }
        return mUdid;
    }

    public void clearBatchCodes() {
        if (mBatchCodes != null) {
            mBatchCodes = null;
        }
        if (mOnCheckedClearListener != null) {
            mOnCheckedClearListener.onCheckedClear();
        }
    }

    public CodeviewBean getBatchCodes() {
        return mBatchCodes;
    }

    public void setBatchCodes(String type) {
        if (TextUtils.isEmpty(type) || mIrCodes == null) {
            mBatchCodes = null;
            if (mOnCheckedClearListener != null) {
                mOnCheckedClearListener.onCheckedClear();
            }
        } else {
            CodeviewBean codeviewBean = mIrCodes.get(type);
            if (codeviewBean != null) {
                mBatchCodes = codeviewBean;
            }
        }

        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChange();
        }
    }

    public void setBatchCodes(CodeviewBean codeviewBean) {
        mBatchCodes = codeviewBean;
        if (mBatchCodes == null && mOnCheckedClearListener != null) {
            mOnCheckedClearListener.onCheckedClear();
        }
        if (mOnCheckedChangeListener != null) {
            mOnCheckedChangeListener.onCheckedChange();
        }
    }

    public boolean isContainCode(IrToBatchItemBean bean) {
        if (BlirConst.IR_SUBDEV_TYPE_AIRCONDITION.equals(mDevType)) {
            if (mSubId == bean.subDevID) {
                String batchIrAirCode = DBUtils.getBatchIrAirCode(bean.codeId);
                mBatchCodes = new CodeviewBean(mDbId, (int) mSubId, mDevType, DBUtils.CODE_TYPE_STATUS, DBUtils.CODE_TYPE_STATUS, DBUtils.CODE_TYPE_STATUS, batchIrAirCode, bean.codeId);
                return true;
            }
        } else {
            Set<String> strings = mIrCodes.keySet();
            for (String type : strings) {
                CodeviewBean codeviewBean = mIrCodes.get(type);
                if (codeviewBean.CodeID == bean.codeId) {
                    mBatchCodes = codeviewBean;
                    return true;
                }
            }
        }
        return false;
    }

    @Override
    public boolean setImage(Bitmap bitmap) {
//        String imageID = getImageID(Consts.SUB_BLIR);
//        getImageCount();
//        if (bitmap == null) {
//            if (ImageManager.RemoveBitmap(imageID)) {
//                return true;
//            }
//        } else {
//            ImageManager.SetBitmap(imageID, bitmap);
//            return true;
//        }
//        return false;


        String imageID = getImageID(Consts.SUB_BLIR);
        if (bitmap != null){
            return HDImageManager.getInstince().setBitmap(imageID,bitmap);
        }

        return false;
    }



    @Override
    public Bitmap getImage() {

//        String imageID = getImageID(Consts.SUB_BLIR);
//        Bitmap bitmap = ImageManager.GetBitmap(imageID);
//        return bitmap;

        String imageID = getImageID(Consts.SUB_BLIR);
        return HDImageManager.getInstince().getBitmap(imageID);

    }

    public String getImagePath(){
        String imageID = getImageID(Consts.SUB_BLIR);
        return HDImageManager.getInstince().getBitmapPath(imageID);
    }

    @Override
    public int getImageCount() {

//        if (ImageManager.ContainsBitmap(getImageID(Consts.SUB_BLIR))) {
//            return 1;
//        }
//        return 0;

        if (HDImageManager.getInstince().containsBitmap(getImageID(Consts.SUB_BLIR))){
            return 1;
        }
        return 0;

    }

}
